/**
 * Project: shuyin-sync
 * 
 * File Created at Mar 13, 2012
 * $Id$
 * 
 * Copyright 2008 6677bank.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * 6677bank Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with 6677bank.com.
 */
package com.bank6677.sync.csm.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.bank6677.sync.csm.dao.UserDao;
import com.bank6677.sync.csm.domain.User;
import com.bank6677.sync.csm.domain.old.AdminInfo;
import com.bank6677.uambank.model.role.Role;
import com.bank6677.uambank.model.role.UserRole;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * TODO Comment of UserDaoImpl
 * 
 * @author liyue
 * @version UserDaoImpl.java Mar 13, 2012 5:37:00 PM
 */

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    @Qualifier("newpayxSqlMapClient")
    private SqlMapClient newpayxClient;

    @Autowired
    @Qualifier("uamSqlMapClient")
    private SqlMapClient uamClient;

    /*
     * (non-Javadoc)
     * @see com.bank6677.sync.csm.dao.UserDao#findAllUsersFromOld()
     */
    @Override
    public List<AdminInfo> findAllUsersFromOld() throws SQLException {
        return newpayxClient.queryForList("user.findAllOld");
    }

    public void save(User user) throws SQLException {
        uamClient.insert("user.addUserInfo", user);
    }

    public void batchInsert(List<User> users) throws SQLException {
        uamClient.startBatch();
        for (User user : users) {

            List<Role> roles = user.getRoles();

            if (CollectionUtils.isEmpty(roles)) {
                continue;//如果改用户无角色则不同步
            }

            uamClient.insert("user.addUserInfo", user);
//            user.setId(userId);
            cascadeInsertUserRole(user);
            cascadeInsertUserSystem(user);
        }
        uamClient.executeBatch();
    }

    /**
     * 插入用户时级联插入用户角色映射表
     * 
     * @param user
     * @throws SQLException
     */
    private void cascadeInsertUserRole(User user) throws SQLException {
        for (Role role : user.getRoles()) {
            UserRole mapping = new UserRole();
            mapping.setRoleId(role.getId());
            mapping.setUserId(user.getId());

            uamClient.insert("user.addUserRoleByUserIdAndRoleId", mapping);
        }
    }

    /**
     * 插入用户时级联插入用户系统映射表
     * 
     * @param user
     * @throws SQLException
     */
    private void cascadeInsertUserSystem(User user) throws SQLException {
        uamClient.insert("user.cascadeInsertUserSystem", user);
    }

}
